
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const { resolve } = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
    mode: 'production',
    performance: false, // 关闭性能分析，提示速度
    plugins: [
        // css 提取到单独css文件
        new MiniCssExtractPlugin({ filename: 'css/[name].[hash:8].css', chunkFilename: '[id].[contenthash].css' }),
        new CopyWebpackPlugin({
            patterns: [
                {
                    from: resolve(__dirname, '../public'),
                    to: resolve(__dirname, '../dist'),
                    toType: "dir",//指定目标的类型
                    noErrorOnMissing: true, // 不生成错误
                    globOptions: {
                        // 忽略文件
                        ignore: ["**/index.html"],
                    },
                    info: {
                        // 跳过terser压缩js
                        minimized: true,
                    },
                },

            ],
        }),
    ],
    optimization: {
        // 修复每次打包hash全部变化
        moduleIds: 'deterministic',
        //添加抽离公共代码插件的配置
        splitChunks: {
            chunks: "all",
            cacheGroups: {
                //打包公共模块
                commons: {
                    chunks: 'initial', //initial表示提取入口文件的公共部分
                    minChunks: 2, //表示提取公共部分最少的文件数
                    minSize: 0, //表示提取公共部分最小的大小
                    name: 'chunk-commons' //提取出来的文件命名
                },
                node_modules: {
                    test: /[\\/]node_modules[\\/]/,
                    name: 'node_modules',
                    chunks: 'initial',
                    priority: 10,
                },
                // 如果项目中使用antd，此时将所有node_modules打包在一起，那么打包输出文件会比较大。
                // 所以我们将node_modules中比较大的模块单独打包，从而并行加载速度更好
                // 如果项目中没有，请删除
                antd: {
                    name: "chunk-antd",
                    test: /[\\/]node_modules[\\/]antd(.*)/,
                    priority: 30,
                },
                // 将react相关的库单独打包，减少node_modules的chunk体积。
                react: {
                    name: "chunk-react",
                    test: /[\\/]node_modules[\\/]react(.*)?[\\/]/,
                    chunks: "initial",
                    priority: 30,
                },
                layouts: {
                    name: "chunk-layouts",
                    test: resolve(__dirname, "../src/layouts"),
                    priority: 40,
                },
            }
        },
        // minimize: true,
        // 压缩js
        minimizer: [
            new TerserPlugin(
                {
                    parallel: true,//设置为 true 或数字值时，表示启用多线程并行处理以加速压缩过程。
                    extractComments: true, // 移除注释
                    terserOptions: {
                        compress: {
                            drop_console: true, // 移除console
                        }
                    }
                }
            ),
            // 压缩css
            new CssMinimizerPlugin(),
        ],
        runtimeChunk: {
            name: (entrypoint) => {
                console.log(entrypoint.name);
                return `runtime~${entrypoint.name}`
            },
        },
    },
}